/**
* @CopyRight xinweitech.cn
* @author wuyahui
* @E-mail wuyahui0124505@163.com
* @version 1.0.0
* @Date  2015年4月3日 下午1:03:40
*/
package com.tstudy.laoshibang.profile;

import java.util.ArrayList;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.StringRes;

import com.tencent.tauth.IUiListener;
import com.tencent.tauth.UiError;
import com.tstudy.laoshibang.R;
import com.tstudy.laoshibang.base.BaseActivity;
import com.tstudy.laoshibang.base.BaseApplication;
import com.tstudy.laoshibang.base.BaseFragment;
import com.tstudy.laoshibang.base.CONSTANT;
import com.tstudy.laoshibang.base.HttpManager;
import com.tstudy.laoshibang.base.HttpManager.LoadImageViewResponse;
import com.tstudy.laoshibang.share.WrapQQ;
import com.tstudy.laoshibang.share.WrapSinaWeibo;
import com.tstudy.laoshibang.share.WrapWechat;
import com.tstudy.laoshibang.utils.CommonUtil;
import com.tstudy.laoshibang.utils.LogUtil;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
@EFragment(R.layout.invite_friend)
public class InviteFriendFragment extends BaseFragment{
	final static String TAG = "invite_friend";
	public InviteFriendFragment() {
		super();
	}
	
	@ViewById(R.id.invite_friend_root)
	LinearLayout mRootLayout;
	String mContent;
	String mEmail;
	
	boolean mIsFirstLoad = true;
	Handler mHandler;
	LayoutInflater mInflater;

	public static void add(int containerViewId) {
		Bundle args = new Bundle();
		args.putInt(CONSTANT.ARGS.FRAGMENT_ID, containerViewId);
		Fragment fragment = Fragment.instantiate(BaseApplication.mContext,InviteFriendFragment_.class.getName(),args);
		((BaseActivity) BaseApplication.mCurrentActivity).add(containerViewId, fragment, TAG);
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		mHandler = new Handler();
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mInflater = inflater;
		super.onCreateView(inflater, container, savedInstanceState);
		LogUtil.d("--mBaseLayout==" + mRootLayout);
		mIsFirstLoad = mRootLayout == null;
		if (mRootLayout != null) {
			((ViewGroup) mRootLayout.getParent()).removeAllViews();
		}
		return mRootLayout;
	}
	
	@AfterViews
	public void initView(){
		if (mIsFirstLoad) {
			mRootLayout.setOnClickListener(null);
		}
	}
	@StringRes(R.string.invite_friend_third_title)
	String title;
	@StringRes(R.string.invite_friend_third_content)
	String content;
	String shareUrl = CONSTANT.APP_DOWNLOAD_URL;
	String mShareImg;
	@Click({R.id.invite_friend_back,R.id.invite_friend_qq,R.id.invite_friend_qzone,R.id.invite_friend_wechat,R.id.invite_friend_wechat_timeline,R.id.invite_friend_sina})
	public void actionClick(View view){
		switch (view.getId()) {
		case R.id.invite_friend_back:
			backAction(mFragmentId);
			break;
		case R.id.invite_friend_qq:
			WrapQQ.getInstance().shareToQQ(title,content, shareUrl,mShareImg, new IUiListener() {
				
				@Override
				public void onError(UiError arg0) {
					BaseApplication.showToast(R.string.share_failed);
				}
				
				@Override
				public void onComplete(Object arg0) {
					BaseApplication.showToast(R.string.share_success);
				}
				
				@Override
				public void onCancel() {
					// TODO Auto-generated method stub
				}
			});
			break;
		case R.id.invite_friend_qzone:
			ArrayList<String> imageUrls =  new ArrayList<String>();
			String imageUrl = mShareImg;
			if (TextUtils.isEmpty(imageUrl)) {
		    	imageUrl = CommonUtil.getLogoImagePath();
				if (TextUtils.isEmpty(imageUrl)) {
					imageUrl = CommonUtil.saveLogoImagePath();
				}
			}
			imageUrls.add(imageUrl);
			WrapQQ.getInstance().shareToQzone(title,content, shareUrl,imageUrls, new IUiListener() {
				
				@Override
				public void onError(UiError arg0) {
					BaseApplication.showToast(R.string.share_failed);
				}
				
				@Override
				public void onComplete(Object arg0) {
					BaseApplication.showToast(R.string.share_success);
				}
				
				@Override
				public void onCancel() {
				}
			});
			break;
		case R.id.invite_friend_wechat:
			if (TextUtils.isEmpty(mShareImg)) {
				Bitmap bitmap = BitmapFactory.decodeResource(BaseApplication.mContext.getResources(), R.drawable.logo);
				WrapWechat.getInstance().shareWebPageToWechat(title,content,shareUrl, bitmap, false);
			}else{
				HttpManager.getInstance().downLoadImage(mShareImg,new LoadImageViewResponse() {
					@Override
					public void loadFinish(Bitmap bitmap) {
						WrapWechat.getInstance().shareWebPageToWechat(title,content,shareUrl, bitmap, false);
					}
				});
			}
			break;
		case R.id.invite_friend_wechat_timeline:
			if (TextUtils.isEmpty(mShareImg)) {
				Bitmap bitmap = BitmapFactory.decodeResource(BaseApplication.mContext.getResources(), R.drawable.logo);
				WrapWechat.getInstance().shareWebPageToWechat(title,content,shareUrl, bitmap, true);
			}else{
				HttpManager.getInstance().downLoadImage(mShareImg,new LoadImageViewResponse() {
					@Override
					public void loadFinish(Bitmap bitmap) {
						WrapWechat.getInstance().shareWebPageToWechat(title,content,shareUrl, bitmap, true);
					}
				});
			}
			break;
		case R.id.invite_friend_sina:
			WrapSinaWeibo.getInstance().shareToWeibo(title,shareUrl,mShareImg);
			break;
		default:
			break;
		}
//		showSoftKeyBoard(minvite_friendContent, false);
//		showSoftKeyBoard(minvite_friendEmail, false);
	}
	@Override
	public void clear() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void initArgs(Bundle args) {
		if (args != null) {
			mFragmentId = args.getInt(CONSTANT.ARGS.FRAGMENT_ID);
		}
	}

}
